﻿using MeterialManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public partial class FrmSetMeterial : Form
    {
        private DelBindDgv _delBindDgv;
        private MeterialCategory _meterialCategory;
        private List<Category> _categories;
        private int _selectCategoryId;
        public FrmSetMeterial(DelBindDgv delBindDgv, int selectCategoryId = 1)
        {
            InitializeComponent();
            _delBindDgv = delBindDgv;
            _selectCategoryId = selectCategoryId;
        }
        public FrmSetMeterial(DelBindDgv delBindDgv, int id, int selectCategoryId = 1) : this(delBindDgv, selectCategoryId)
        {
            _meterialCategory = MeterialCategory.ListAll().Find(m => m.Id == id);
        }

        private void FrmSetMeterial_Load(object sender, EventArgs e)
        {
            _categories = new List<Category>();
            _categories = Category.GetListCategory();
            cbxCategoryName.DataSource = _categories;
            cbxCategoryName.DisplayMember = "CategoryName";
            cbxCategoryName.ValueMember = "CategoryId";
            if (_meterialCategory != null)
            {
                tbMeterialName.Text = _meterialCategory.MeterialName;
                tbUnit.Text = _meterialCategory.Unit;
                tbStocks.Text = _meterialCategory.Stocks.ToString();
                tbRemark.Text = _meterialCategory.Remark;
                cbxCategoryName.SelectedValue = _meterialCategory.CategoryId;
            }
            else
            {
                cbxCategoryName.SelectedValue = _selectCategoryId;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string categoryCode = _categories.Find(m => m.CategoryId == (int)cbxCategoryName.SelectedValue).CategoryCode;
            string meterialName = tbMeterialName.Text.Trim();
            string unit = tbUnit.Text.Trim();
            int stocks = int.Parse(tbStocks.Text);
            string remark = tbRemark.Text.Trim();
            if(_meterialCategory == null)
            {
                    Meterial meterial = new Meterial()
                    {
                        CategoryCode = categoryCode,
                        MeterialName = meterialName,
                        Unit = unit,
                        Stocks = stocks,
                        Remark = remark
                    };
                    Meterial.Insert(meterial);
                    MessageBox.Show("添加成功！");
                
            }
            else
            {
                    Meterial meterial = new Meterial()
                    {
                        Id=_meterialCategory.Id,
                        CategoryCode = categoryCode,
                        MeterialName = meterialName,
                        Unit = unit,
                        Stocks = stocks,
                        Remark = remark
                    };
                    Meterial.Update(meterial);
                    MessageBox.Show("修改成功！");
            }
            _delBindDgv();
            this.Close();
        }
    }
}
